package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.competition.dao.UserDao;
import com.competition.entity.User;
import com.competition.mapper.UserMapper;
import com.competition.resp.ResponseResult;
import com.competition.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/getUser")
    public ResponseResult getUser(@RequestBody Map<String, Object> params) {
        Object openId = params.get("openId");
        if (openId == null) {
            return new ResponseResult(400, "获取用户信息失败", null);
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", openId);
        User one = null;
        try {
            one = userService.getOne(wrapper);
            //返回结果填充
            Map<String, String> map = new HashMap<>();
            map.put("name", one.getUserName());
            map.put("sex", one.getUserSex());
            map.put("idcard", one.getUserIdcare());
            map.put("phoneNumber", one.getUserPhone());
            return new ResponseResult(200, "获取成功", map);
        } catch (Exception e) {
            return new ResponseResult(300, "获取失败", null);
        }

    }

    @GetMapping("/getAllUser")
    public ResponseResult getAllUser(@RequestParam("roleID") String string) {
        if (string == null){
            return new ResponseResult(400, "参数异常", null);
        }
        try {
            List<UserDao> allByRole = userMapper.getAllByRole(string);
            return new ResponseResult(200, "获取成功", allByRole);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(300, "获取失败", null);
        }
    }


}
